package com.hbun.cxcy.controller;


import com.hbun.cxcy.common.utils.R;
import com.hbun.cxcy.entity.Dept;
import com.hbun.cxcy.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mahaunyu
 * @since 2022-01-24
 */
@CrossOrigin
@RestController
@RequestMapping("/cxcy/dept")
public class DeptController {

    @Autowired
    DeptService deptService;

    @GetMapping("/list")
    public R list(){
        List<Dept> list = deptService.list();
        return R.ok().put("data",list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{deptId}")
    public R info(@PathVariable("deptId") Long deptId){
        Dept dept = deptService.getById(deptId);
        return R.ok().put("dept", dept);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("cxcyweb:dept:save")
    public R save(@RequestBody Dept dept){
        deptService.save(dept);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("cxcyweb:dept:update")
    public R update(@RequestBody Dept dept){
        deptService.updateById(dept);
        return R.ok();
    }

    /**
     * 删除,此处不用逻辑删除，直接删便可
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("cxcyweb:dept:delete")
    public R delete(@RequestBody Long[] deptIds){
        System.out.println(Arrays.toString(deptIds));
//        deptService.removeByIdsWithCustom(Arrays.asList(deptIds));
        deptService.removeByIds(Arrays.asList(deptIds));
        return R.ok();
    }
}

